import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import "amfe-flexible";
import { createPinia } from 'pinia';
// pinia持久化缓存插件
import piniaPersist from 'pinia-plugin-persistedstate';
import NutUI from "@nutui/nutui";
import "@nutui/nutui/dist/style.css";
import { IconFont } from '@nutui/icons-vue';
import '@nutui/icons-vue/dist/style_iconfont.css';
import "@/common/js/touchEmulator.js";

declare global {
  interface Window {
    [key: string]: any
  }
}

const app = createApp(App);

// 注册长按事件
app.directive('longpress', function (el, binding) {
  let timer: any = null;
  let start = function (e: any) {
    // 如果是点击事件，不启动计时器，直接返回
    if (e.type === 'click') {
      return
    }
    if (timer == null) {
      // 创建定时器 ( 2s之后执行长按功能函数 )
      timer = setTimeout(function () {
        //执行长按功能函数
        binding.value()
      }, 2000)
    }
  }
  var cancel = function () {
    if (timer !== null) {
      clearTimeout(timer)
      timer = null
    }
  }

  // 添加事件监听器
  el.addEventListener("touchstart", start);

  // 取消计时器
  el.addEventListener("click", cancel);
  el.addEventListener("touchend", cancel);
})

const pinia = createPinia();
pinia.use(piniaPersist);
app.use(pinia);
app.use(NutUI);
app.use(IconFont);

app.use(router);
app.mount("#app");